package com.stone.tool.easyexcel;

import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * https://easyexcel.opensource.alibaba.com/docs/current/quickstart/read
 *
 * @author stone
 * @date 2022/5/16 16:33
 */
public class EasyExcelUtil {

    public static void main(String[] args) throws Exception {
        writeFileTemplate();
    }

    /**
     * 导出excel-基于模版
     */
    public static void writeFileTemplate() throws IOException {
        String result = "C:\\Users\\stone\\Desktop\\" + System.currentTimeMillis() + ".xlsx";
        // 模版: 填充list的模板中{.}多了个点=表示list
        ClassPathResource classPathResource = new ClassPathResource("template/export01.xlsx");
        InputStream is = classPathResource.getInputStream();
        try (ExcelWriter excelWriter = EasyExcel.write(result).withTemplate(is).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            List<UserTemplate> list = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                UserTemplate userTemplate = new UserTemplate();
                userTemplate.setName("name" + i);
                userTemplate.setAge(String.valueOf(i));
                list.add(userTemplate);
            }
            // 填充
            excelWriter.fill(list, writeSheet);
        }
    }

    /**
     * 导出excel
     */
    public static void writeFileCommon() {
        String result = "C:\\Users\\stone\\Desktop\\" + System.currentTimeMillis() + ".xlsx";
        try (ExcelWriter excelWriter = EasyExcel.write(result, UserTemplate.class).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet("模板11").build();
            // 分页
            List<UserTemplate> list = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                UserTemplate userTemplate = new UserTemplate();
                userTemplate.setName("name" + i);
                userTemplate.setAge(String.valueOf(i));
                userTemplate.setInputStream(FileUtil.getInputStream("C:\\Users\\stone\\Desktop\\图片\\3.jpg"));
                list.add(userTemplate);
            }
            excelWriter.write(list, writeSheet);
        }
    }

    /**
     * 读取excel
     */
    public static void readFile() {
        // 写法1：
        String fileName = "C:\\Users\\stone\\Desktop\\1753175763638.xlsx";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(fileName, UserTemplate.class, new DemoDataListener()).sheet().doRead();

        System.out.println("================================");
        System.out.println("================================");

        // 写法2
        // 一个文件一个reader
        try (ExcelReader excelReader = EasyExcel.read(fileName, UserTemplate.class, new DemoDataListener()).build()) {
            // 构建一个sheet 这里可以指定名字或者no
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            // 读取一个sheet
            excelReader.read(readSheet);
        }
    }

    /**
     * 基于class，输出excel模版
     */
    public static void generateTemplate() {
        String result = "C:\\Users\\stone\\Desktop\\" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(result)
                .autoCloseStream(true)
                .excelType(ExcelTypeEnum.XLSX)
                .head(UserTemplate.class)
                .sheet("用户模版")
                .doWrite(Collections.emptyList());
    }


}
